//
//  SelectVideoClipView.m
//  OntheRoadV4
//
//  Created by ZhangYanhua on 15/3/4.
//  Copyright (c) 2015年 EricHuang. All rights reserved.
//

#import "SelectVideoClipView.h"
#import "QWImageMangerConfig.h"
#define IMAGE_BORDER_WIDHT      12.0f

@interface SelectVideoClipView() {
    CGPoint touchedBeginPoint;
    
    CGFloat _curProgress;
}

@property (nonatomic, strong) UIView *leftMaskView;
@property (nonatomic, strong) UIView *rightMaskView;

@property (nonatomic, strong) UIImageView *centerImgView;
@property (nonatomic, strong) UIView *progressView;

@end

@implementation SelectVideoClipView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonSetup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonSetup];
    }
    return self;
}

- (void)commonSetup
{
    self.exclusiveTouch = YES;

    _leftMaskView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, self.bounds.size.height)];
    _leftMaskView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
    _leftMaskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
    
    _rightMaskView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width - 1.0f, 0.0f, 1.0f, self.bounds.size.height)];
    _rightMaskView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
    _rightMaskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
    
    _centerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height)];
    _centerImgView.backgroundColor = [UIColor clearColor];
    _centerImgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _centerImgView.image = [QWImageMangerConfig getStrenchImageHasCache:@"bg_video_progress"];

    _progressView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, - 3.0f, 6.0f, 6.0f + self.bounds.size.height)];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _progressView.backgroundColor = [UIColor whiteColor];
    _progressView.layer.cornerRadius = 2.0f;
    
    [self addSubview:_leftMaskView];
    [self addSubview:_rightMaskView];
    [self addSubview:_centerImgView];
    [self addSubview:_progressView];
}

- (void)updateViewWithMinWidth:(CGFloat)minWidth maxWidth:(CGFloat)maxWidth margin:(CGFloat)margin
{
    _minWidth = minWidth;
    _maxWidth = maxWidth;
    _leftRightMargin = margin;

    _leftMaskView.frame = CGRectMake(0.0f, 0.0f, margin - IMAGE_BORDER_WIDHT, self.bounds.size.height);

    _centerImgView.frame = CGRectMake(CGRectGetMaxX(_leftMaskView.frame),
                                      0.0f,
                                      _maxWidth + 2 * IMAGE_BORDER_WIDHT,
                                      self.bounds.size.height);

    _rightMaskView.frame = CGRectMake(CGRectGetMaxX(_centerImgView.frame),
                                      0.0f,
                                      self.bounds.size.width - CGRectGetMaxX(_centerImgView.frame),
                                      self.bounds.size.height);

    CGFloat startX = (CGRectGetWidth(_centerImgView.frame) - 2 * IMAGE_BORDER_WIDHT - _progressView.frame.size.width) * _curProgress;
    startX += CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT;
    _progressView.frame = CGRectMake(startX, -3.0f, 6.0f, self.bounds.size.height + 6.0f);
}

- (void)updatePlayProgress:(CGFloat)progress
{
    _curProgress = progress;
    
    CGFloat startX = (CGRectGetWidth(_centerImgView.frame) - 2 * IMAGE_BORDER_WIDHT - _progressView.frame.size.width) * _curProgress;
    startX += CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT;
    _progressView.frame = CGRectMake(startX, -3.0f, 6.0f, self.bounds.size.height + 6.0f);
}

- (void)showPlayPorgressWithAnimation:(BOOL)animated
{
    [UIView animateWithDuration:0.25 * (animated ? 1 : 0)
                     animations:^{
                         self.progressView.alpha = 1.0f;
                     }];
}

- (void)hidePlayProgressWithAnimation:(BOOL)animated
{
    [UIView animateWithDuration:0.25 * (animated ? 1 : 0)
                     animations:^{
                         self.progressView.alpha = 0.0f;
                     }];
}

- (CGFloat)currentStartX
{
    return CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT - _leftRightMargin;
}

- (CGFloat)currentEndX
{
    return CGRectGetMaxX(_centerImgView.frame) - IMAGE_BORDER_WIDHT - _leftRightMargin;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    touchedBeginPoint = point;
    
    CGFloat extendWidth = floorf(_minWidth / 2.0f);

    _touchedOnLeftView = NO;
    _touchedOnRightView = NO;

    if (point.x <= CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT + extendWidth) {
        _touchedOnLeftView = YES;
    } else if (point.x >= _rightMaskView.frame.origin.x - extendWidth - IMAGE_BORDER_WIDHT) {
        _touchedOnRightView = YES;
    }

    if (_touchedOnRightView || _touchedOnLeftView) {
        if ([self.delegate respondsToSelector:@selector(clipViewTouchedDidBegin:)]) {
            [self.delegate clipViewTouchedDidBegin:self];
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:self];
    CGPoint prePoint = [touch previousLocationInView:self];
    
    [self updateViewCurrentLocation:curPoint previousLocation:prePoint];

    if (_touchedOnLeftView || _touchedOnRightView) {
        if ([self.delegate respondsToSelector:@selector(clipViewTouchedDidEnd:)]) {
            [self.delegate clipViewTouchedDidEnd:self];
        }
    }

    _touchedOnLeftView = NO;
    _touchedOnRightView = NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:self];
    CGPoint prePoint = [touch previousLocationInView:self];

    [self updateViewCurrentLocation:curPoint previousLocation:prePoint];

    if (_touchedOnRightView || _touchedOnLeftView) {
        if ([self.delegate respondsToSelector:@selector(clipViewTouchedDidEnd:)]) {
            [self.delegate clipViewTouchedDidEnd:self];
        }
    }
    
    _touchedOnLeftView = NO;
    _touchedOnRightView = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:self];
    CGPoint prePoint = [touch previousLocationInView:self];
    
    [self updateViewCurrentLocation:curPoint previousLocation:prePoint];

    if (_touchedOnLeftView || _touchedOnRightView) {
        if ([self.delegate respondsToSelector:@selector(clipViewTouchedDidMove:)]) {
            [self.delegate clipViewTouchedDidMove:self];
        }
    }
}

- (void)updateViewCurrentLocation:(CGPoint)curLocation previousLocation:(CGPoint)preLocation
{
    CGFloat delta = curLocation.x - preLocation.x;
    if (_touchedOnLeftView) {
        CGFloat startX = CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT + delta;
        startX = MIN(startX, _rightMaskView.frame.origin.x - IMAGE_BORDER_WIDHT - _minWidth);
        startX = MAX(startX, _rightMaskView.frame.origin.x - IMAGE_BORDER_WIDHT - _maxWidth);
        startX = MAX(startX, _leftRightMargin);

        CGRect frame = _leftMaskView.frame;
        frame.size.width = startX - IMAGE_BORDER_WIDHT;
        _leftMaskView.frame = frame;

        frame = _centerImgView.frame;
        frame.size.width = CGRectGetMaxX(frame) - CGRectGetMaxX(_leftMaskView.frame);
        frame.origin.x = CGRectGetMaxX(_leftMaskView.frame);
        _centerImgView.frame = frame;
        
        frame = _progressView.frame;
        frame.origin.x = roundf(_centerImgView.frame.origin.x + IMAGE_BORDER_WIDHT);
        _progressView.frame = frame;
    } else {
        CGFloat startX = CGRectGetMinX(_rightMaskView.frame) - IMAGE_BORDER_WIDHT + delta;
        CGFloat maxEndX = _maxWidth + _leftRightMargin;
        startX = MIN(startX, CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT + _maxWidth);
        startX = MIN(startX, maxEndX);
        startX = MAX(startX, CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT + _minWidth);

        CGRect frame = _rightMaskView.frame;
        frame.origin.x = startX + IMAGE_BORDER_WIDHT;
        frame.size.width = CGRectGetMaxX(self.bounds) - frame.origin.x;
        _rightMaskView.frame = frame;

        frame = _centerImgView.frame;
        frame.size.width = startX + IMAGE_BORDER_WIDHT - frame.origin.x;
        _centerImgView.frame = frame;
        
        frame = _progressView.frame;
        frame.origin.x = roundf(CGRectGetMaxX(_centerImgView.frame) - IMAGE_BORDER_WIDHT - _progressView.frame.size.width);
        _progressView.frame = frame;
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    
    CGFloat extendWidth = floorf(_minWidth / 2.0f);
    if (point.x <= CGRectGetMaxX(_leftMaskView.frame) + IMAGE_BORDER_WIDHT + extendWidth ||
        point.x >= _rightMaskView.frame.origin.x - extendWidth - IMAGE_BORDER_WIDHT) {
        return hitView;
    } else {
        return nil;
    }
}

@end
